package com.neusoft.yuejian_01.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.activity.FeedBackActivity;
import com.neusoft.yuejian_01.activity.UpdateInfoActivity;
import com.neusoft.yuejian_01.javabean.UserBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.ConstPhoto;
import com.neusoft.yuejian_01.model.UserModel;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends Fragment implements RetrofitListener {
    private TextView UserName,meInfo,meLike,meNews,meSet,help,gift;
    private ImageView UserPic;
    private Context context;
    private View view=null;
    private  GiftFragment giftFragment;
    private   MeSetPictureFragment  mesetPictureFragment;
    private   MyNewsFragment myNewsFragment;
    private FragmentManager manager;
    private android.support.v4.app.FragmentTransaction transaction;

    public MeFragment() {
        // Required empty public constructor
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_me, container, false);

    }
    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        UserName = (TextView) view.findViewById(R.id.UuserName);
        meInfo = (TextView) view.findViewById(R.id.meInfo);
        meLike = (TextView) view.findViewById(R.id.like);
        meNews = (TextView) view.findViewById(R.id.news);
        meSet = (TextView) view.findViewById(R.id.tiezi);
        help = (TextView) view.findViewById(R.id.help);
        gift = (TextView) view.findViewById(R.id.gift);
        UserPic=(ImageView) view.findViewById(R.id.mepic);

        UserModel userModel=new UserModel();
        userModel.userInfo(getUser_id(),this);

        //修改个人信息
        meInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),UpdateInfoActivity.class);
                startActivityForResult(intent,1 );//requestCode
            }
        });


        help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),FeedBackActivity.class);
                startActivityForResult(intent,1 );//requestCode
            }
        });

        //兑换礼品
        manager = getChildFragmentManager();
        gift.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                giftFragment = new GiftFragment();
                replace(giftFragment);
            }
        });

      //我的信息
        manager = getChildFragmentManager();
        meNews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myNewsFragment = new MyNewsFragment();
                replace(myNewsFragment);
            }
        });

        //我的发帖
        manager = getChildFragmentManager();
        meSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mesetPictureFragment = new MeSetPictureFragment();
                replace( mesetPictureFragment);
            }
        });
    }

    private void replace(Fragment fragment) {
        transaction = manager.beginTransaction();
        transaction.replace(R.id.meF, fragment);
        transaction.commit();
    }

    public int getUser_id()
    {
        SharedPreferences sp;
        sp =getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        return sp.getInt("u_id", -1);
    }

    @Override
    public void onSuccess(Object object, int flag) {
        UserBean userInfo = (UserBean)object;
        UserName.setText(userInfo.getU_name());
        String headpic= ConstPhoto.BASE_URL+ConstPhoto.SetPictureFile+userInfo.getU_pic();
        Picasso.with(MeFragment.this.getActivity()).load( headpic).into(UserPic);
    }
    @Override
    public void onFail() {
    }
}

